import {defineStore} from "pinia";
import type {fileType} from "./type";
import {
    ListByReportParams,
    rpDirDataType,
} from "@/api/file/type";
import {
    getRpAndDirList,
    batchRemoveFile,
    listByReport, uploadReportFile
} from "@/api/file";
import {ElMessage, ElMessageBox} from "element-plus";
import {list} from "postcss";

let useFileStore = defineStore('file', {
    state: (): fileType => {
        return {
            reportLeftItem:{},
            menuList:[],
            diguiOver:'',
            tableLoading:false,
            fileReportList: [],
            reportFileCreateTime: [],
            reportFileName: '',
            fileRemoveIds: [],
            defaultLeftOpends:[],
            fileFormData: new FormData(),
            limit: 50,
            page: 1,
            total: 0,
        }
    },
    actions: {
        //获取左侧菜单
        setReportLeftItem(item: rpDirDataType) {
            this.reportLeftItem = item
        },
        async useGetRpAndDirList() {
            let res = await getRpAndDirList()
            if (res.code === 0) {
                this.menuList = res.data
                this.diguiOver = ''
                this.findReportId(this.menuList, 'type', 'report', 'item')
                this.setReportLeftItem(this.diguiOver)
                this.setDefaultLeftOpends(this.menuList)
            } else {
                ElMessage({
                    message: res.msg,
                    type: 'error',
                })
            }

        },


        //获取文件列表
        async useListByReport() {
            this.tableLoading = true
            let params: ListByReportParams
            params = {
                reportId: this.reportLeftItem.id,
                fileName: this.reportFileName,
                reportTime: '',
                createStartTime: this.reportFileCreateTime.length > 0 ? this.reportFileCreateTime[0] : '',
                createEndTime: this.reportFileCreateTime.length > 1 ? this.reportFileCreateTime[1] : '',
                page: this.page,
                limit: this.limit
            }

            let res = await listByReport(params)
            if (res.code === 0) {
                this.fileReportList = res.data.list
                this.total = res.data.total
                console.log(res.data.list)
            } else {
                ElMessage({
                    message: res.msg,
                    type: 'error',
                })
            }
            this.tableLoading = false
        },

        //批量删除文件
        async useBatchRemoveFile(ids: number[]) {
            // console.log(ids)
            let res = await batchRemoveFile({ids})
            if (res.code === 0) {
                ElMessage({
                    message: '删除成功',
                    type: 'success',
                })
            } else {
                ElMessage({
                    message: '删除失败' + res.msg,
                    type: 'error',
                })
            }
            this.useListByReport()
        },

        //上传文件
        async useUploadReportFile() {
            let res = await uploadReportFile(this.fileFormData)
            if (res.code === 0) {
                ElMessage({
                    message: '上传成功',
                    type: 'success',
                })
            } else {
                ElMessage({
                    message: '上传失败',
                    type: 'error',
                })
            }
            this.useListByReport()
            this.fileFormData = new FormData()
        },
        findReportTitleItem(list: any[]) {

        },
        setDefaultLeftOpends(list: any[]) {
            for (let i = 0; i < list.length; i++) {
                if (list[i].children) {
                    this.defaultLeftOpends.push(list[i].id)
                    this.setDefaultLeftOpends(list[i].children)
                }
            }

        },

        //@ts-ignore
        findReportId(list: any, key: string, value: string | number, returnName: string) {
            for (let i = 0; i < list.length; i++) {
                if (list[i][key] == value) {
                    if (this.diguiOver != '') {
                        return this.diguiOver
                    } else {
                        if (returnName == 'item') {
                            this.diguiOver = list[i]
                        } else {
                            this.diguiOver = list[i][returnName]
                        }
                        break;
                    }
                } else {
                    return this.findReportId(list[i].children, key, value, returnName)

                }
            }
        },

    },
    getters: {}
})

export default useFileStore
